package com.hnucm.c202001020214;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gyf.immersionbar.ImmersionBar;

public class Login extends BaseActivity {

    private Button button;
    private TextView textView;
    UserDaoDatabase userDaoDatabase;
    UserDao userDao;
    EditText e1,e2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        e1=findViewById(R.id.username2);
        e2=findViewById(R.id.password2);

        userDaoDatabase = Room.databaseBuilder(this,UserDaoDatabase.class,"Mytest")
                .allowMainThreadQueries()//允许在主线程中调用
                .build();
        //返回接口的实现类（动态生成的类）
        userDao = userDaoDatabase.getUserDao();

        ImmersionBar.with(this)
                //.statusBarColor(R.color.blue)
                .init();

        button = findViewById(R.id.blogin);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到首页
                String s= userDao.findByTel(e1.getText().toString());
                if(e2.getText().toString().equals(s)){

                    Intent intent = new Intent(Login.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
                else{
                    Toast.makeText(Login.this,"密码错误",Toast.LENGTH_LONG).show();
                }
            }
        });


        textView = findViewById(R.id.benroll);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳到注册界面
                Intent intent = new Intent(Login.this, Enroll.class);
                startActivity(intent);
//                finish();
            }
        });
    }
}